import styled from 'styled-components';
import React, { CSSProperties, useEffect, useState } from 'react';
import { Card, Descriptions, Button } from 'antd';
import {
    FooterToolbar,
    PageContainer,
    ProForm,
    ProFormDateRangePicker,
    ProFormDigit,
    ProFormRadio,
    ProFormSelect,
    ProFormItem,
    ProFormText,
    ProFormTextArea,
    ProFormUploadButton,
    ProLayout,
} from '@ant-design/pro-components';

import { SharePanel } from '../../../../components';
import { getNowFormatDate, uuid, homePath } from '../../../../utils';
import { ConnectorDataSource, MetaDataDomain, metaDataGetMetaDomain, qiniuUploadLocalFile } from '../../../../services';
import { useCallback } from 'react';
import { transformDomainExport } from '@/services/local';

export interface MetaDataSharePanelProps {
    style?: CSSProperties;
    domainIdentifier: string;
}

export const MetaDataSharePanel: React.FC<MetaDataSharePanelProps> = (props) => {

    const [domain, setDomain] = useState<MetaDataDomain | null>(null);

    useEffect(() => {
        if (!props.domainIdentifier) return;
        metaDataGetMetaDomain(props.domainIdentifier).then((domain) => {
            setDomain(domain);
        });
    }, [props.domainIdentifier]);

    return (
        <Container style={props.style}>
            <StyledCard
                style={{
                }}
                title={'域分享'}
                styles={{
                    body: {
                        padding: '0px 2px'
                    }
                }}
            >
                <SharePanel
                    {...props}
                    objectType={'DOMAIN'}
                    init={{
                        name: domain ? domain.name : ''
                    }}
                    referId={props.domainIdentifier}
                    getDownload={async (objectType, referId, identifier) => {
                        //导出到文件
                        let home = await homePath();
                        let exportId = uuid();
                        let file = `${home}/temp/${exportId}.zip`;
                        try {
                            let home = await homePath();

                            let source: ConnectorDataSource = {
                                id: 0,
                                protocol: 'sqlite',
                                url: `sqlite:${home}/rrg01.sqlite`,
                                database_type: 'sqlite',
                            };

                            let res = await transformDomainExport(source, file, [{ domain_identifier: props.domainIdentifier }], {
                                ignore_errs: true,
                                file_options: {
                                    version: 'V1.0.0',
                                    format: 'sqlite'
                                }
                            });
                            //上传文件
                            let uploadResult = await qiniuUploadLocalFile(file, `g01/share/images/${objectType}/${getNowFormatDate('')}`);
                            console.log(uploadResult);
                            return uploadResult.key;
                        } catch (err) {
                            console.error(err);
                        }
                        return null;
                    }}
                >
                </SharePanel>
            </StyledCard>
        </Container>
    );
}

const Container = styled.div`
    width: 100%;
    height: 100%;
    padding: 6px 6px;
    display: flex;
    gap: 10px;
`;

const StyledCard = styled(Card)`
    width: 100%;
`
